import { createRouter, createWebHistory } from 'vue-router'
import LayOut from '@/views/layout/index.vue'
import { useUserStore } from '@/stores'

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/',
      component: LayOut,
      redirect: '/book',
      children: [
        {
          path: '/book',
          name: 'book',
          component: () => import("@/views/bookList/index.vue")
        },
        {
          path: '/orderAll',
          name: 'orderAll',
          component: () => import("@/views/orders/OrderAll.vue")
        },
        {
          path: '/noPay',
          name: 'noPay',
          component: () => import("@/views/orders/OrderNoPay.vue")
        },
        {
          path: '/pay',
          name: 'pay',
          component: () => import("@/views/orders/OrderPay.vue")
        },
        {
          path: '/finish',
          name: 'finish',
          component: () => import("@/views/orders/OrderFinish.vue")
        },
        {
          path: '/userinfo',
          name: 'userinfo',
          component: () => import("@/views/userinfo/index.vue")
        },
        {
          path: '/createOrder',
          name: 'createOrder',
          component: () => import("@/views/pay/createOrder.vue")
        },
        {
          path: '/payOrder',
          name: 'payOrder',
          component: () => import("@/views/pay/payOrder.vue")
        },
      ]
    },
    {
      path: '/login',
      component: () => import('../views/UserLogin/UserLoginView.vue'),
    },
      {
          path: '/register',
          component: () => import('../views/UserReg/UserRegView.vue'),
      },
  ],
})

// 登录访问拦截
router.beforeEach((to, from) => {
  const userStore = useUserStore()
  if (!userStore.userInfo.token && to.path !== '/login' && to.path !== '/register') return '/login'
})


export default router
